#coding:utf-8
import requests
from  requests.exceptions import ConnectionError
from urllib.parse import  urlencode

base_url='http://weixin.sogou.com/weixin?'

headers={
    'Request URL':'http://weixin.sogou.com/weixin?query=%E4%B8%AD%E5%8D%B0%E6%B4%9E%E6%9C%97&type=2&page=1&ie=utf8',
    'Cookie':'SUV=1515636285111571; SMYUV=1515636285112901; UM_distinctid=160e2f65ebe292-03680141f13fe2-454c0b2b-144000-160e2f65ebf469; ABTEST=0|1520471373|v1; IPLOC=CN3301; SUID=B1758AD34942910A000000005AA08D4D; SUID=B1758AD33220910A000000005AA08D4E; weixinIndexVisited=1; sct=1; SNUID=BE7A84DD0E0B69D25AB0BECE0F7DC2FE; ppinf=5|1520471456|1521681056|dHJ1c3Q6MToxfGNsaWVudGlkOjQ6MjAxN3x1bmlxbmFtZTo0OlJ5YW58Y3J0OjEwOjE1MjA0NzE0NTZ8cmVmbmljazo0OlJ5YW58dXNlcmlkOjQ0Om85dDJsdUNwOHA2bFdQcE9QOGk3aGhWZ21GQlVAd2VpeGluLnNvaHUuY29tfA; pprdig=spsQpnSqhSJqIh8hPPy99O5R-ji-qa6Ie7N0PMBt_dyeQ56Cw0aJ1jmfB2YmLyBOHAWQqKAybyD99c38AZ5FGnvTsn_p-J_bEFTDZ-O8JfygJCaG8uPei-szFostMKdtMi_UdDIximHO4IdlU6vhdkOQhq-zuG8tDnEs0OAwLB0; sgid=10-33962951-AVqgjaCIHqWtNJnk0xeCzp8; ppmdig=15204726880000009dd597b4efa27c902a6b2f0b62c3490b; JSESSIONID=aaa5p0aJQ75-19r3Ozwhw',
    'Host':'weixin.sogou.com',
    'Upgrade-Insecure-Requests':1,
    'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36',

}

keyword='中印洞朗'

max_count=15


def get_html(url,count=1):
    print('crawing',url)
    print('try time',count)
    global  proxy#全局变量
    if count>=max_count:
        return None
    try:
        response=requests.get(url,headers=headers,allow_redirects=False)
        if response.status_code==200:
            return response.text
        if response.status_code==302:
            print(url+'response 302')
    except ConnectionError:
        return get_html()#连接错误

def get_index(keyword,page):#得到每个分页页面的html源码
    data={
        'query':keyword,
        'type':2,
        'page':page
    }
    quries=urlencode(data)
    url=base_url+quries
    html=get_html(url)
    return html

def main():
    for page in range(1,101):
        print(get_html(keyword,page))

if __name__ == '__main__':
    main()